﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.IO;

namespace EC
{
    public partial class _Default : System.Web.UI.Page
    {
        private ECCryptosystem system = null;

        protected void Page_Load(object sender, EventArgs e)
        {

        }
		
        private void alert(string msg)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("alert('");
            sb.Append(msg.Replace("\n", "\\n").Replace("\r", "").Replace("'", "\\'"));
            sb.Append("');");
            ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "showalert", sb.ToString(), true);
        }

        protected void generateBtn_Click(object sender, EventArgs e)
        {
            var p = new BigInteger(TextBox1.Text, 10);
            var q = new BigInteger(TextBox2.Text, 10);
            var m = new BigInteger(TextBox3.Text, 10);
            try
            {
                ListBox1.Items.Clear();
                var group = new ECGroup(p, q, m);
                foreach (ECPoint item in group.Generate())
                {
                    ListBox1.Items.Add(item.ToString());
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        }

        protected void determineGBtn_Click(object sender, EventArgs e)
        {
            ListBox2.Items.Clear();
            ECPoint g = null;
            g = new ECPoint(ListBox1.SelectedValue);

            var p = new BigInteger(TextBox1.Text, 10);
            var q = new BigInteger(TextBox2.Text, 10);
            var m = new BigInteger(TextBox3.Text, 10);
            var group = new ECGroup(p, q, m);
            system = new ECCryptosystem(group);
            if (g == null)
                return;
            var kGGroup = system.DeterminekG(g);
            foreach (ECPoint item in kGGroup)
            {
                ListBox2.Items.Add(item.ToString());
            }
        }

        protected void exchangeBtn_Click(object sender, EventArgs e)
        {
            ECPoint g = null;
            g = new ECPoint(ListBox2.SelectedValue);

            var p = new BigInteger(TextBox1.Text, 10);
            var q = new BigInteger(TextBox2.Text, 10);
            var m = new BigInteger(TextBox3.Text, 10);
            var group = new ECGroup(p, q, m);
            system = new ECCryptosystem(group);
            int na = int.Parse(TextBox4.Text);
            int nb = int.Parse(TextBox5.Text);

            var k = system.KeyExchange(na, nb, g);

            TextBox10.Text = string.Concat("Wymiana kluczy:\n\nG = {", g.X, ", " + g.Y + "}\nPa = ", system.Pa, "\nPb = ", system.Pb, "\nK = na*Pb = ", k[0], "\nK = nb*Pa = ", k[1]);
        
        }

        protected void PmBtn_Click(object sender, EventArgs e)
        {
            ECPoint Pm = null;
            Pm = new ECPoint(ListBox1.SelectedValue);
            if (Pm == null)
                return;
            TextBox7.Text = Pm.ToString();
        }

        protected void Cm1Btn_Click(object sender, EventArgs e)
        {
            ECPoint Cm1 = null;
            Cm1 = new ECPoint(ListBox1.SelectedValue);
            if (Cm1  == null)
                return;
            TextBox8.Text = Cm1.ToString();
        }

        protected void Cm2Btn_Click(object sender, EventArgs e)
        {
            ECPoint Cm2 = null;
            Cm2 = new ECPoint(ListBox1.SelectedValue);
            if (Cm2 == null)
                return;
            TextBox9.Text = Cm2.ToString();
        }

        protected void encipherBtn_Click(object sender, EventArgs e)
        {
            ECPoint g = null;
            g = new ECPoint(ListBox2.SelectedValue);

            var p = new BigInteger(TextBox1.Text, 10);
            var q = new BigInteger(TextBox2.Text, 10);
            var m = new BigInteger(TextBox3.Text, 10);
            var group = new ECGroup(p, q, m);
            system = new ECCryptosystem(group);


            int nb = int.Parse(TextBox5.Text);
            int k = int.Parse(TextBox6.Text);
            var kGGroup = system.DeterminekG(g);

            if (k >= system.C)
                return;

            var Cm = system.Encipher(new ECPoint(TextBox7.Text), k, nb);
            TextBox10.Text = string.Concat("Szyfrowanie:\n\n", "Cm1 = " + Cm[0] + '\n', "Cm2 = " + Cm[1]);
        }

        protected void dencipherBtn_Click(object sender, EventArgs e)
        {
            ECPoint g = null;
            g = new ECPoint(ListBox2.SelectedValue);

            var p = new BigInteger(TextBox1.Text, 10);
            var q = new BigInteger(TextBox2.Text, 10);
            var m = new BigInteger(TextBox3.Text, 10);
            var group = new ECGroup(p, q, m);
            system = new ECCryptosystem(group);

            int nb = int.Parse(TextBox5.Text);
            int k = int.Parse(TextBox6.Text);

            var kGGroup = system.DeterminekG(g);

            if (k >= system.C)
                return;

            var Pm = system.Decipher(new ECPoint(TextBox8.Text), new ECPoint(TextBox9.Text), k, nb);
            TextBox10.Text = string.Concat("Deszyfrowanie:\n\n", "Pm = " + Pm);
        }

        protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
		
        }           
    }
}
